package cn.dwy2002.binaryhandwritingserver.config;


import cn.dwy2002.binaryhandwritingserver.interceptor.JwtValidateInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * MVC配置器类
 *
 * @author 杜伟毅
 * @version 1.0
 * @since 2023/10/09
 */
@Configuration
@RequiredArgsConstructor
public class InterceptorConfig implements WebMvcConfigurer {
    private final JwtValidateInterceptor jwtValidateInterceptor;
//    @Value("${cors}")
//    private String cors;
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins(cors)
//                .allowedMethods("*")
//                .allowCredentials(true);
//    }
    /**
     * 添加拦截器
     *
     * @param registry 注册表
     * @author 杜伟毅
     * @since 2023/10/9 0009 23:03
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册拦截器
        InterceptorRegistration registration = registry.addInterceptor(jwtValidateInterceptor);
        // 只拦截以/admin开头的路径
        registration.addPathPatterns("/admin/**");
        // 保留对特殊路径的放行（可选，根据需要添加）
//                .excludePathPatterns("/error")
    }
}
